package com.shop2cn.iapi.agentproducts.testcase;

import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.BatchDeleteProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.service.BatchDeleteProductCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.utils.AgentUsers;

/**
 * 批量删除商品 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_BatchDeleteProduct {
	private static BatchDeleteProductBean BatchDeleteProductBean;
	private static BatchDeleteProductCall BatchDeleteProductCall;
	// 渠道商
	private static int sellerId = AgentUsers.rootUserId;
	private static String sellerName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	// 渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";

	// vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	// vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	// vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量删除商品");
	}

	@Before
	public void caseUp() {
		BatchDeleteProductBean = new BatchDeleteProductBean();
		BatchDeleteProductCall = new BatchDeleteProductCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(BatchDeleteProductCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchDeleteProduct_01() {
		Logger.start(true, "渠道商删除商品,删除成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
					sellerName, false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			BatchDeleteProductBean.setProductIdList(Arrays.asList(productId));
			BatchDeleteProductBean.setUserId(sellerId);
			BatchDeleteProductCall.setData(BatchDeleteProductBean);
			BatchDeleteProductCall.callService();
			Logger.verifyEquals("200", BatchDeleteProductCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchDeleteProduct_02() {
		Logger.start(true, "代理商删除商品,删除成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
					sellerName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(agentUserId1, agentUserName1, productId, sellerId, false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			BatchDeleteProductBean.setUserId(agentUserId1);
			BatchDeleteProductBean.setProductIdList(Arrays.asList(agentProductId));
			BatchDeleteProductCall.setData(BatchDeleteProductBean);
			BatchDeleteProductCall.callService();
			Logger.verifyEquals("200", BatchDeleteProductCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchDeleteProduct_03() {
		Logger.start(true, "vip渠道->vip1->vip2，vip渠道商删除商品，删除成功,vip1和VIP2对应商品信息删除");
		try {
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId1, vipAgentUserName1, productId, vipRootUserId, true);

			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId2, vipAgentUserName2, agentProductId, vipAgentUserId1, false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);
			BatchDeleteProductBean.setUserId(vipRootUserId);
			BatchDeleteProductBean.setProductIdList(Arrays.asList(productId));
			BatchDeleteProductCall.setData(BatchDeleteProductBean);
			BatchDeleteProductCall.callService();
			Logger.verifyEquals("200", BatchDeleteProductCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchDeleteProduct_04() {
		Logger.start(true, "vip渠道->vip1->vip2，vip1删除商品，删除成功，vip2商品删除");
		try {
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId1, vipAgentUserName1, productId, vipRootUserId, true);

			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService
					.initImportAgentProduct(vipAgentUserId2, vipAgentUserName2, agentProductId, vipAgentUserId1, false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);
			BatchDeleteProductBean.setUserId(vipAgentUserId1);
			BatchDeleteProductBean.setProductIdList(Arrays.asList(agentProductId));
			BatchDeleteProductCall.setData(BatchDeleteProductBean);
			BatchDeleteProductCall.callService();
			Logger.verifyEquals("200", BatchDeleteProductCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchDeleteProduct_05() {
		Logger.start(true, "商品id不存在,删除失败");
		try {
			BatchDeleteProductBean.setUserId(vipAgentUserId1);
			BatchDeleteProductBean.setProductIdList(Arrays.asList("123123"));
			BatchDeleteProductCall.setData(BatchDeleteProductBean);
			BatchDeleteProductCall.callService();
			Logger.verifyEquals("200", BatchDeleteProductCall.getString("code"), "验证code");

			JsonObject productMsgList = BatchDeleteProductCall.getJsonObject().get("data").getAsJsonObject().get("productMsgList").getAsJsonArray().get(0).getAsJsonObject();
			Logger.verifyEquals("123123", productMsgList.get("productName").getAsString(), "商品上架失败");
			Logger.verifyEquals("商品不存在", productMsgList.get("msg").getAsString(), "商品上架失败");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchDeleteProduct_06() {
		Logger.start(true, "用户id不存在，删除失败");
		try {
			BatchDeleteProductBean.setUserId(111);
			BatchDeleteProductBean.setProductIdList(Arrays.asList("123"));
			;
			BatchDeleteProductCall.setData(BatchDeleteProductBean);
			BatchDeleteProductCall.callService();
			Logger.verifyEquals("201", BatchDeleteProductCall.getString("code"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchDeleteProduct_07() {
		Logger.start(true, "普通渠道->普通代理，渠道商删除商品,删除成功，代理商对应商品删除");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(sellerId,
					sellerName, true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService
					.initImportAgentProduct(agentUserId1, agentUserName1, productId, sellerId, false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			BatchDeleteProductBean.setUserId(sellerId);
			BatchDeleteProductBean.setProductIdList(Arrays.asList(productId));
			BatchDeleteProductCall.setData(BatchDeleteProductBean);
			BatchDeleteProductCall.callService();
			Logger.verifyEquals("200", BatchDeleteProductCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_BatchDeleteProduct_08() {
		Logger.start(true, "保存商品删除，删除成功");
		try {
			AddAgentProductBean agentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,
					vipRootUserName, true);
			agentProductBean.setSaleType(2);
			String productId = AddAgentProductCallService.AddAgentProduct(agentProductBean);
			BatchDeleteProductBean.setUserId(vipRootUserId);
			BatchDeleteProductBean.setProductIdList(Arrays.asList(productId));
			BatchDeleteProductCall.setData(BatchDeleteProductBean);
			BatchDeleteProductCall.callService();
			Logger.verifyEquals("200", BatchDeleteProductCall.getString("code"), "验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}